﻿Imports System.Data.OleDb
Imports System.IO
Imports System.ComponentModel
Public Class Frm_Scale_Send

    Public Enum TeraziTip
        ishida = 0
        cas = 1
        DigiSM300 = 2
        CASLP500 = 3
        casLp1 = 4
    End Enum
    Private terazi_tipi As TeraziTip
    Public HataVerdi As Boolean = False
    Public GonderilenKayitSayisi As Integer
    Public Property TeraziTipi() As TeraziTip
        Get
            Return terazi_tipi
        End Get
        Set(ByVal value As TeraziTip)
            terazi_tipi = value
        End Set
    End Property

    Sub New(ByVal terazi_dosyasi As String, ByVal depono As String, ByVal terazi As TeraziTip)
        InitializeComponent()
        Terazi_Dosya = terazi_dosyasi
        Depo_No = depono
        Me.TeraziTipi = terazi
    End Sub

    Dim Terazi_Dosya As String
    Dim Depo_No As String

    Private Prg_Min As Integer
    Public Property Min() As Integer
        Get
            Return Prg_Min
        End Get
        Set(ByVal Value As Integer)
            Prg_Min = Value
        End Set
    End Property

    Private Prg_Max As Integer
    Public Property Max() As Integer
        Get
            Return Prg_Max
        End Get
        Set(ByVal Value As Integer)
            Prg_Max = Value
        End Set
    End Property

    Private Prg_Percent As Integer
    Public Property Percent() As Integer
        Get
            Return Prg_Percent
        End Get
        Set(ByVal Value As Integer)
            Prg_Percent = Value
        End Set
    End Property
    Public WithEvents bgworker As New BackgroundWorker

    Public Sub dowork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgworker.DoWork
        Dim prm() As String = CType(e.Argument, String())
        If Me.TeraziTipi = TeraziTip.ishida Then
            Ishida_Urun_Exp(prm(0), prm(1), prm(2))
        ElseIf Me.TeraziTipi = TeraziTip.cas Then
            Cas_Urun_Exp(prm(0), prm(1), prm(2), "#000.00")
        ElseIf Me.TeraziTipi = TeraziTip.casLp1 Then
            CasLP1_Urun_Exp(prm(0), prm(1), prm(2), "#000.00")
        ElseIf Me.TeraziTipi = TeraziTip.CASLP500 Then
            Cas5000_Urun_Exp(prm(0), prm(1), prm(2))
        Else
            DigiSM300_Urun_Exp(prm(0), prm(1), prm(2))
        End If
    End Sub

    Private Sub bgworker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgworker.ProgressChanged
        ProgressBar1.Maximum = Prg_Max
        If e.ProgressPercentage > Prg_Max Then
            ProgressBar1.Value = Prg_Max
        Else
            ProgressBar1.Value = e.ProgressPercentage
        End If
        Label1.Text = "Aktarım devam ediyor...  " & ProgressBar1.Value
    End Sub

    Public Sub worker_complete(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgworker.RunWorkerCompleted
        If Not e.Error Is Nothing Then
            HataVerdi = True
            Label1.Text = "Aktarılamadı !"
        Else
            Label1.Text = "Aktarım Tamamlandı."
        End If
    End Sub

    Public Sub Start_Export(ByVal Tarih As DateTime, ByVal Depo_kod As String, ByVal File_Path As String)
        Dim str(2) As String
        str(0) = Tarih
        str(1) = Depo_kod
        str(2) = File_Path
        bgworker.WorkerReportsProgress = True
        bgworker.WorkerSupportsCancellation = True
        bgworker.RunWorkerAsync(str)
    End Sub

    Private Sub DigiSM300_Urun_Exp(ByVal Tarih As DateTime, ByVal Depo_kod As String, ByVal File_Path As String)
        Try
            Dim dt As New DataTable
            Dim cn2 As New OleDbConnection(Cnn_String)

            Dim str_sql As String = "SELECT  count(*)  FROM Q_DepoFiyat RIGHT JOIN (TBL_STOK INNER JOIN TBL_BARKOD ON TBL_STOK.stokid=TBL_BARKOD.stokid) ON Q_DepoFiyat.barkod=TBL_BARKOD.barkod  WHERE (((TBL_BARKOD.barkodicerik)=1 Or (TBL_BARKOD.barkodicerik)=2)) Or (((Mid(TBL_BARKOD.barkod,1,2))='25'));"
            Dim adapter As New OleDbDataAdapter(str_sql, cn2)
            adapter.SelectCommand.Parameters.Add("@depoid", OleDbType.VarChar)
            adapter.SelectCommand.Parameters.Item("@depoid").Value = Depo_kod

            cn2.Open()
            adapter.Fill(dt)

            Prg_Min = 0
            Prg_Max = Val(dt.Rows(0)(0))
            GonderilenKayitSayisi = Prg_Max
            bgworker.ReportProgress(1, 0)
            Prg_Percent = 0

            Dim cmd As New OleDbCommand
            cmd.Connection = cn2
            If RadioButton1.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
            ElseIf RadioButton2.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi2"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@tarih", OleDbType.Date)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
                cmd.Parameters.Item("@tarih").Value = Tarih
            End If
            cmd.CommandTimeout = 999999

            Dim rd As OleDbDataReader = cmd.ExecuteReader
            Dim i As Integer = 0

            Dim dosya_write As StreamWriter = New StreamWriter(File_Path, False, System.Text.Encoding.Default)
            Dim barkod As String = ""
            Dim stok_adi As String = ""
            Dim satis_fiyat As Decimal = 0
            Dim plu_no As String = ""
            Dim convFiyat As String = ""


            While rd.Read
                If bgworker.CancellationPending = True Then
                    Exit Sub
                End If

                barkod = Mid(rd("barkod"), 1, 13)
                stok_adi = rd("stokad")

                If Not rd("depofiyat1") Is DBNull.Value Then
                    If rd("depofiyat1") > 0 Then
                        satis_fiyat = Mid(Format(Val(rd("depofiyat1")), "000000.00"), 1, 10)
                    Else
                        satis_fiyat = Mid(Format(Val(rd("genelfiyat1")), "000000.00"), 1, 10)
                    End If
                Else
                    If Not rd("genelfiyat1") Is DBNull.Value Then
                        satis_fiyat = Mid(Format(Val(rd("genelfiyat1")), "000000.00"), 1, 10)
                    End If
                End If

                plu_no = Microsoft.VisualBasic.Strings.Right(barkod, 5)
                convFiyat = satis_fiyat
                convFiyat = convFiyat.Replace(".", "")
                convFiyat = convFiyat.Replace(",", "")
                convFiyat = convFiyat.Trim


                If convFiyat.Length < 6 Then
                    For a As Integer = 1 To 6 - convFiyat.Length
                        convFiyat = "0" & convFiyat
                    Next
                End If

                If barkod.Length < 13 Then
                    For a As Integer = 1 To 13 - barkod.Length
                        barkod = barkod & "0"
                    Next
                End If

                If plu_no.Length < 5 Then
                    For a As Integer = 1 To 5 - plu_no.Length
                        plu_no = "0" & plu_no
                    Next
                End If

                dosya_write.WriteLine("WHFA" & plu_no & vbTab & "B" & convFiyat & vbTab & "C17" & vbTab & "E5" & vbTab & "F" & barkod & "1" & vbTab & "G997" & vbTab & "H000" & vbTab & "K000" & vbTab & "Q7400" & vbTab)
                dosya_write.WriteLine("WHFA" & plu_no & vbTab & "B1" & vbTab & "C7" & vbTab & "D" & stok_adi & vbTab)

                Prg_Percent += 1
                bgworker.ReportProgress(Prg_Percent, 0)


            End While
            dosya_write.Close()
            cn2.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Ishida_Urun_Exp(ByVal Tarih As DateTime, ByVal Depo_kod As String, ByVal File_Path As String)
        Try
            Dim ds As New DataSet
            Dim cn2 As New OleDbConnection(Cnn_String)
            Dim trh As String = ""
            If RadioButton1.Checked = True Then
                trh = "01/01/1900"
            Else
                trh = Now.Year & "/" & Now.Month & "/" & Now.Day & " 00:00:00"
            End If
            Dim str_sql As String = "SELECT  count(*)  FROM Q_DepoFiyat RIGHT JOIN (TBL_STOK INNER JOIN TBL_BARKOD ON TBL_STOK.stokid=TBL_BARKOD.stokid) ON Q_DepoFiyat.barkod=TBL_BARKOD.barkod  WHERE (((TBL_BARKOD.barkodicerik)=1 Or (TBL_BARKOD.barkodicerik)=2)) Or (((Mid(TBL_BARKOD.barkod,1,2))='25'));"
            Dim adapter As New OleDbDataAdapter(str_sql, cn2)
            adapter.SelectCommand.Parameters.Add("@depoid", OleDbType.VarChar)
            adapter.SelectCommand.Parameters.Item("@depoid").Value = Depo_kod

            cn2.Open()
            adapter.Fill(ds)

            Prg_Min = 0
            Prg_Max = Val(ds.Tables(0).Rows(0)(0))
            GonderilenKayitSayisi = Prg_Max
            bgworker.ReportProgress(1, 0)
            Prg_Percent = 0

            Dim cmd As New OleDbCommand
            cmd.Connection = cn2
            If RadioButton1.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
            ElseIf RadioButton2.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi2"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@tarih", OleDbType.Date)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
                cmd.Parameters.Item("@tarih").Value = Tarih
            End If

            cmd.CommandTimeout = 999999
            Dim rd As OleDbDataReader = cmd.ExecuteReader
            Dim i As Integer = 0

            Dim sw3 As StreamWriter = New StreamWriter(File_Path, False, System.Text.Encoding.Default)
            Dim barcode, stock_adi, price, Cprice, plutus, omur As String


            While rd.Read

                If bgworker.CancellationPending = True Then
                    Exit Sub
                End If

                barcode = Format(Val(rd("barkod")), "0000000")
                stock_adi = Mid(rd("stokad").ToString, 1, 25)

                If Not rd("depofiyat1") Is DBNull.Value Then
                    If rd("depofiyat1") > 0 Then
                        price = Mid(Format(Val(rd("depofiyat1")), "000000.00"), 1, 10)
                        Cprice = Replace(price, ",", "")
                        Cprice = Replace(Cprice, ".", "")
                    Else
                        price = Mid(Format(Val(rd("genelfiyat1")), "000000.00"), 1, 10)
                        Cprice = Replace(price, ",", "")
                        Cprice = Replace(Cprice, ".", "")
                    End If
                Else
                    If Not rd("genelfiyat1") Is DBNull.Value Then
                        price = Mid(Format(Val(rd("genelfiyat1")), "000000.00"), 1, 10)
                        Cprice = Replace(price, ",", "")
                        Cprice = Replace(Cprice, ".", "")
                    End If
                End If


                plutus = Microsoft.VisualBasic.Strings.Right(barcode, 4)
                omur = "000"

                If Len(stock_adi) < 25 Then
                    stock_adi += Space(25 - Len(stock_adi))
                Else
                    stock_adi = Mid(stock_adi, 1, 25)
                End If

                sw3.WriteLine(Mid(barcode, 1, 7) & stock_adi & plutus & Cprice & "0100001" & omur)

                Prg_Percent += 1
                bgworker.ReportProgress(Prg_Percent, 0)

            End While
            sw3.Close()
            cn2.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Cas_Urun_Exp(ByVal Tarih As DateTime, ByVal Depo_kod As String, ByVal File_Path As String, ByVal fiyatformat As String)
        Try
            Dim trh As String = ""
            If RadioButton1.Checked = True Then
                trh = "01/01/1900"
            Else
                trh = Now.Year & "/" & Now.Month & "/" & Now.Day & " 00:00:00"
            End If
            Dim ds As New DataSet
            Dim cn2 As New OleDbConnection(Cnn_String)
            Dim str_sql As String = "SELECT Count(*)  FROM TBL_STOK INNER JOIN TBL_BARKOD ON TBL_STOK.stokid = TBL_BARKOD.stokid  WHERE (((TBL_BARKOD.barkodicerik)=1 Or (TBL_BARKOD.barkodicerik)=2) AND ((TBL_STOK.tarih)>=#" & trh & "#))"
            Dim adapter As New OleDbDataAdapter(str_sql, cn2)

            cn2.Open()
            adapter.Fill(ds)

            Prg_Min = 0
            Prg_Max = Val(ds.Tables(0).Rows(0)(0))
            Prg_Percent = 0

            Dim cmd As New OleDbCommand
            cmd.Connection = cn2
            cmd.CommandTimeout = 999999
            If RadioButton1.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi order by stokid"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
            ElseIf RadioButton2.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi2 order by stokid"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@tarih", OleDbType.Date)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
                cmd.Parameters.Item("@tarih").Value = Tarih
            End If

            Dim rd As OleDbDataReader = cmd.ExecuteReader
            Dim plu As Integer = 1

            Dim sw3 As StreamWriter = New StreamWriter(File_Path, False, System.Text.Encoding.Default)
            Dim barcode, stock_adi, price, Cprice, plutus, omur As String

            While rd.Read
                If bgworker.CancellationPending = True Then
                    Exit Sub
                End If

                barcode = Format(Val(Microsoft.VisualBasic.Right(rd(1), 5)), "00000")
                stock_adi = Mid(rd(2).ToString, 1, 28)
                If Not rd(4) Is DBNull.Value Then
                    If rd(4) > 0 Then
                        price = Mid(Format(Val(rd(4)), fiyatformat), 1, 8)
                    Else
                        price = Mid(Format(Val(rd(3)), fiyatformat), 1, 8)
                    End If
                Else
                    price = Mid(Format(Val(rd(3)), fiyatformat), 1, 8)
                End If

                Cprice = Replace(price, ",", "")
                plutus = Microsoft.VisualBasic.Right(rd(1), 4)

                If Cprice.Length < 8 Then
                    Cprice = Cprice & Space(8 - Cprice.Length)
                End If

                If stock_adi.Length < 28 Then
                    stock_adi = stock_adi & Space(28 - stock_adi.Length)
                End If

                If rd(4) Is DBNull.Value Then
                    omur = "365"
                Else
                    omur = Format(Val(rd(4)), "000")
                End If

                sw3.WriteLine(plutus & vbTab & stock_adi & vbTab & stock_adi & vbTab & Cprice & vbTab & omur & vbTab & "    " & vbTab & barcode & " " & vbTab & "1 " & vbTab & "   " & vbTab)

                Prg_Percent += 1
                bgworker.ReportProgress(Prg_Percent, 0)

                plu += 1

            End While
            sw3.Close()

            cn2.Close()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub CasLP1_Urun_Exp(ByVal Tarih As DateTime, ByVal Depo_kod As String, ByVal File_Path As String, ByVal fiyatformat As String)
        Try
            Dim trh As String = ""
            If RadioButton1.Checked = True Then
                trh = "01/01/1900"
            Else
                trh = Now.Year & "/" & Now.Month & "/" & Now.Day & " 00:00:00"
            End If
            Dim ds As New DataSet
            Dim cn2 As New OleDbConnection(Cnn_String)
            Dim str_sql As String = "SELECT Count(*)  FROM TBL_STOK INNER JOIN TBL_BARKOD ON TBL_STOK.stokid = TBL_BARKOD.stokid  WHERE (((TBL_BARKOD.barkodicerik)=1 Or (TBL_BARKOD.barkodicerik)=2) AND ((TBL_STOK.tarih)>=#" & trh & "#))"
            Dim adapter As New OleDbDataAdapter(str_sql, cn2)

            cn2.Open()
            adapter.Fill(ds)

            Prg_Min = 0
            Prg_Max = Val(ds.Tables(0).Rows(0)(0))
            Prg_Percent = 0

            Dim cmd As New OleDbCommand
            cmd.Connection = cn2
            cmd.CommandTimeout = 999999
            If RadioButton1.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi order by stokid"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
            ElseIf RadioButton2.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi2 order by stokid"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@tarih", OleDbType.Date)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
                cmd.Parameters.Item("@tarih").Value = Tarih
            End If

            Dim rd As OleDbDataReader = cmd.ExecuteReader
            Dim plu As Integer = 1

            Dim sw3 As StreamWriter = New StreamWriter(File_Path, False, System.Text.Encoding.Default)
            Dim barcode, stock_adi, price, Cprice, plutus, omur As String

            While rd.Read
                If bgworker.CancellationPending = True Then
                    Exit Sub
                End If

                barcode = Format(Val(Microsoft.VisualBasic.Right(rd(1), 5)), "000000")

                stock_adi = Mid(rd(2).ToString, 1, 28)
                If Not rd(4) Is DBNull.Value Then
                    If rd(4) > 0 Then
                        price = Mid(Format(Val(rd(4)), fiyatformat), 1, 8)
                    Else
                        price = Mid(Format(Val(rd(3)), fiyatformat), 1, 8)
                    End If
                Else
                    price = Mid(Format(Val(rd(3)), fiyatformat), 1, 8)
                End If

                Cprice = Replace(price, ",", "")
                plutus = CInt(Microsoft.VisualBasic.Right(rd(1), 4))
                plutus = Space(4 - plutus.Length) & plutus

                If Cprice.Length < 8 Then
                    Cprice = Space(8 - Cprice.Length) & Cprice
                End If

                If stock_adi.Length < 28 Then
                    stock_adi = stock_adi & Space(28 - stock_adi.Length)
                End If

                'If rd(4) Is DBNull.Value Then
                '    omur = "365"
                'Else
                'omur = Format(Val(rd(4)), "000")
                'End If
                omur = "  0"

                sw3.WriteLine(plutus & vbTab & stock_adi & vbTab & Mid(stock_adi, 1, 27) & vbTab & Cprice & vbTab & omur & vbTab & "   0" & vbTab & barcode & " " & vbTab & "00" & vbTab & "  0" & vbTab)

                Prg_Percent += 1
                bgworker.ReportProgress(Prg_Percent, 0)

                plu += 1

            End While
            sw3.Close()

            cn2.Close()

        Catch ex As Exception

        End Try
    End Sub


    Private Sub Cas5000_Urun_Exp(ByVal Tarih As DateTime, ByVal Depo_kod As String, ByVal File_Path As String)
        Try
            Dim trh As String = ""
            If RadioButton1.Checked = True Then
                trh = "01/01/1900"
            Else
                trh = Now.Year & "/" & Now.Month & "/" & Now.Day & " 00:00:00"
            End If
            Dim ds As New DataSet
            Dim cn2 As New OleDbConnection(Cnn_String)
            Dim str_sql As String = "SELECT Count(*)  FROM TBL_STOK INNER JOIN TBL_BARKOD ON TBL_STOK.stokid = TBL_BARKOD.stokid  WHERE (((TBL_BARKOD.barkodicerik)=1 Or (TBL_BARKOD.barkodicerik)=2) AND ((TBL_STOK.tarih)>=#" & trh & "#))"
            Dim adapter As New OleDbDataAdapter(str_sql, cn2)

            cn2.Open()
            adapter.Fill(ds)

            Prg_Min = 0
            Prg_Max = Val(ds.Tables(0).Rows(0)(0))
            Prg_Percent = 0

            Dim cmd As New OleDbCommand
            cmd.Connection = cn2
            cmd.CommandTimeout = 999999
            If RadioButton1.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi order by Right(barkod,4)"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
            ElseIf RadioButton2.Checked = True Then
                cmd.CommandText = "SELECT * FROM Q_Terazi2 order by Right(barkod,4)"
                cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                cmd.Parameters.Add("@tarih", OleDbType.Date)
                cmd.Parameters.Item("@depoid").Value = Depo_kod
                cmd.Parameters.Item("@tarih").Value = Tarih
            End If

            Dim rd As OleDbDataReader = cmd.ExecuteReader
            Dim plu As Integer = 1

            Dim sw3 As StreamWriter = New StreamWriter(File_Path, False, System.Text.Encoding.Default)
            Dim barcode, stock_adi, price, Cprice, plutus, omur As String

            While rd.Read
                If bgworker.CancellationPending = True Then
                    Exit Sub
                End If

                barcode = Format(Val(Microsoft.VisualBasic.Right(rd(1), 5)), "00000")
                stock_adi = Mid(rd(2).ToString, 1, 28)
                If Not rd(4) Is DBNull.Value Then
                    If rd(4) > 0 Then
                        price = Mid(Format(Val(rd(4)), "#0.00"), 1, 8)
                    Else
                        price = Mid(Format(Val(rd(3)), "#0.00"), 1, 8)
                    End If
                Else
                    price = Mid(Format(Val(rd(3)), "#0.00"), 1, 8)
                End If

                Cprice = Replace(price, ",", "")
                plutus = CInt(Microsoft.VisualBasic.Right(rd(1), 4)).ToString().PadRight(4, " ")

                If Cprice.Length < 8 Then
                    Cprice = Cprice & Space(8 - Cprice.Length)
                End If

                If stock_adi.Length < 28 Then
                    stock_adi = stock_adi & Space(28 - stock_adi.Length)
                End If

                'If rd(4) Is DBNull.Value Then
                '    omur = "365"
                'Else
                '    omur = Format(Val(rd(4)), "000")
                'End If
                omur = "  0"

                sw3.WriteLine(plutus & vbTab & stock_adi & vbTab & stock_adi & vbTab & Cprice & vbTab & omur & vbTab & "0   " & vbTab & barcode & " " & vbTab & " 0" & vbTab & "0  " & vbTab)

                Prg_Percent += 1
                bgworker.ReportProgress(Prg_Percent, 0)

                plu += 1

            End While
            sw3.Close()

            cn2.Close()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Aktarım Başlandı"
        Dim tarih As New DateTime(Now.Year, Now.Month, Now.Day, 0, 0, 0)
        Start_Export(tarih, Depo_No, Terazi_Dosya)
    End Sub

    Private Sub Frm_Scale_Send_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RadioButton2.Checked = True
        RadioButton1.Checked = False
        DateTimePicker1.Value = Now
    End Sub
End Class
